<?php
namespace app\admin\controller;

use app\common\helper\Encrypt;
use app\common\model\project_model\PaymentConfigModel;
use app\common\model\WechatAppletModel;
use app\yxwadmin\controller\Admin;
use app\admin\model\validate\PaymentConfig as ValidatePayment;

class PaymentConfig extends Admin
{

    public $error_msg = '';

    /**
     * 首页
     * @return string
     * @throws \think\Exception
     */
    public function index()
    {
        return $this->view->fetch();
    }

    /**
     * 初始化表格
     * 关键字搜索
     * 分页
     * @throws
     * @return mixed
     */
    public function getList()
    {
        $limit = input('limit','0');
        $offset = input('offset','0');
        $sortName = input('sortName','id');
        $sortOrder = input('sortOrder','desc');
        $keyword = input('keywords','');

        $model = new PaymentConfigModel();
        if ($keyword) {
            $count = $model->where('applet_name|applet_app_id|mch_id|key', 'like', '%' . $keyword . '%')->count();
            $list = $model
               ->where('applet_name|applet_app_id|mch_id|key', 'like', '%' . $keyword . '%')
               ->order([$sortName => $sortOrder])
               ->limit($offset . ',' . $limit)
               ->select();
        } else {
            $count = $model->count(); //查询满足要求的总记录数
            $list = $model
                ->order(array($sortName=>$sortOrder))
                ->limit($offset.','.$limit)
                ->select();
        }

        $data['total'] = $count;
        $data['rows'] = $list;
        return $data;
    }

    public function add() {
        if (request()->isPost()) {
            $validate = new ValidatePayment();
            $data = $_REQUEST;
            $data['created_time'] = date('Y-m-d H:i:s', time());
            $data['updated_time'] = date('Y-m-d H:i:s', time());
            if (empty($data['key'])) {
                return json(['code' => 0, 'msg' => '商户KEY不能为空']);
            }
            $data['key'] = Encrypt::encode(trim($data['key']), config('encrypt_secret_key'));
            $data['mch_id'] = trim($data['mch_id']);
            $res = $validate->check($data);
            if (!$res) {
                $this->error_msg = $validate->getError();
                return json(['code' => 0, 'msg' => $this->error_msg, 'data' => null]);
            }
            $model = new PaymentConfigModel($data);
            $res = $model->allowField(true)->save();
            if (!$res) {
                $this->error_msg = $model->getError();
                return json(['code' => 0, 'msg' => $this->error_msg, 'data' => null]);
            }
            return json(['code' => 1, 'msg' => '新增成功', 'data' => null]);

        } else {
            $this->assign('applet_list', WechatAppletModel::appletList('photo_albums_bless'));

            return $this->fetch();
        }
    }

    public function edit() {
        $id = $_REQUEST['id'];
        $model = new PaymentConfigModel();

        if (request()->isPost()) {
            $validate = new ValidatePayment();
            $data = $_REQUEST;
            $data['updated_time'] = date('Y-m-d H:i:s', time());
            $data['mch_id'] = trim($data['mch_id']);
            $res = $validate->scene('edit')->check($data);
            if (!$res) {
                $this->error_msg = $validate->getError();
                return json(['code' => 0, 'msg' => $this->error_msg, 'data' => null]);
            }

            $res = $model->allowField(true)->save($data,['id' => $id]);
            if ($res === false) {
                $this->error_msg = $model->getError();
                return json(['code' => 0, 'msg' => $this->error_msg, 'data' => null]);
            }

            return json(['code' => 1, 'msg' => '编辑成功', 'data' => null]);

        } else {
            $this->assign('applet_list', WechatAppletModel::appletList('photo_albums_bless'));
            $Rs = $model->where(['id' => $id])->find();
            $this->assign('Rs', $Rs);
            return $this->fetch();
        }
    }

    /**
     * 修改key
     */
    public function update_key()
    {
        if (request()->isPost()) {
            $validate = new ValidatePayment();
            $data = $_REQUEST;
            $data['updated_time'] = date('Y-m-d H:i:s', time());
            if (empty($data['key'])) {
                return json(['code' => 0, 'msg' => '商户KEY不能为空']);
            }
            $data['key'] = Encrypt::encode(trim($data['key']), config('encrypt_secret_key'));
            $res = $validate->scene('update_key')->check($data);
            if (!$res) {
                $this->error_msg = $validate->getError();
                return json(['code' => 0, 'msg' => $this->error_msg, 'data' => null]);
            }
            $model = new PaymentConfigModel();
            $res = $model->allowField(true)->save($data, ['id' => $data['id']]);
            if ($res === false) {
                $this->error_msg = $model->getError();
                return json(['code' => 0, 'msg' => $this->error_msg, 'data' => null]);
            }

            return json(['code' => 1, 'msg' => '编辑成功', 'data' => null]);
        } else {
            $id = input('id/d', '');
            $this->assign('id', $id);
            return $this->fetch();
        }
    }

}